<template>
<transition name="popup-slide-up">
  <div class="ebook-popup-list" v-show="fontFamilyVisible && menuVisible">
    <div class="ebook-popup-title">
      <div class="ebook-popup-title-icon" @click="hide">
        <span class="icon-arrow_down"></span>
      </div>
      <span class="ebook-popup-title-text">选择字体</span>
    </div>
    <div class="ebook-popup-list-wrapper">
      <div class="ebook-popup-item" v-for="(item, index) in fontFamilyList" :key="index" @click="setFontFamily(item.font)">
        <div class="ebook-popup-item-text" :class="{'selected': isSelected(item)}">{{item.font}}</div>
        <div class="ebook-popup-item-check" v-if="isSelected(item)">
          <span class="icon-gou_huaban1"></span>
        </div>
      </div>
    </div>
  </div>
  </transition>
</template>

<script>
import { FONT_FAMILY } from '../../utils/book'
import {ebookMixin} from '../../utils/mixin'
// 测试setLocalStorage是否有效
import {setLocalStorage, getLocalStorage, removeLocalStorage, clearLocalStorage, saveFontFamily} from '../../utils/localStorage'
export default {
  mixins: [ebookMixin],
  data () {
    return {
      fontFamilyList: FONT_FAMILY
    }
  },
  methods: {
    setFontFamily(font) {
      // 对 defaultFontFamily 进行赋值即可
      this.setDefaultFontFamily(font)
      // this.fileName 文件名
      console.log(this.fileName, font);
      // 调用saveFontFamily() 方法, 在localStore写入文件名和字体
      saveFontFamily(this.fileName, font)
      // 页面字体进行响应变化
      if(font === 'Default'){
        this.currentBook.rendition.themes.font('Times New Roman')
      } else{
        this.currentBook.rendition.themes.font(font)
      }
    },
    isSelected(item){
      return this.defaultFontFamily === item.font
    },
    hide() {
      this.setFontFamilyVisible(false)
    }
  },
  mounted() {
    // 设置localStorage, 看看效果
    // setLocalStorage(this.fileName, this.defaultFontFamily)
    // 取值
    // console.log(getLocalStorage(this.fileName))
    // 删除
    // removeLocalStorage(this.fileName)
    // 清除
    // clearLocalStorage()
  }
}
</script>

<style lang="scss" scoped>
@import '../../assets/styles/global.scss';
.ebook-popup-list {
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 300;
  // height: 100%;
  width: 100%;
  box-shadow: 0 px2rem(-4) px2rem(6) rgba(0,0,0, 0.1);
  background: white;
  .ebook-popup-title{
    position: relative;
    padding: px2rem(15);
    box-sizing: border-box;
    border-bottom: px2rem(1) solid #b8b9bb;
    text-align: center;
    @include center;
    .ebook-popup-title-icon {
      position: absolute;
      left: px2rem(15);
      top: 0;
      height: 100%;
      font-size: px2rem(16);
      font-weight: bold;
      @include center;
    }
    .ebook-popup-title-text {
      font-size: px2rem(14);
      font-weight: bold;
    }
  }
  .ebook-popup-list-wrapper{
    .ebook-popup-item{
      display: flex;
      padding: px2rem(15);
      .ebook-popup-item-text {
        flex: 1;
        font-size: px2rem(14);
        text-align: left;
        &.selected {
          color: #346cb9;
          font-weight: bold;
        }
      }
      .ebook-popup-item-check {
        flex: 1;
        text-align: right;
        font-weight: bold;
        font-size: px2rem(14);
        color: #346cb9;
       
      }
    }
  }
}
</style>